package capstoneproject.dudiachi.Controller;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import capstoneproject.dudiachi.DataAccess.DBHelper;
import capstoneproject.dudiachi.Model.OldDistrict;
import capstoneproject.dudiachi.Model.OldProvince;
import capstoneproject.dudiachi.Model.TagType;

public class OldDistrictManager {
	private ArrayList<OldDistrict> listOldDistrict;
	private DBHelper helper;

	private OldProvinceManager opm;
	/**
	 * <pre>
	 * タッグ�?�データ列�?�行�?�インデックスを�?��?��?��?�。
	 * </pre>
	 */
	private List<TagType> listTag;

	/**
	 * <pre>
	 * コンストラクタ。 
	 * 祭りリストを�?��?��?�をロード。
	 * </pre>
	 * 
	 * @param ctx
	 */
	public OldDistrictManager(Context ctx) {
		super();
		helper = new DBHelper(ctx);
		listOldDistrict = new ArrayList<OldDistrict>();
		listTag = new ArrayList<TagType>();
		loadListOldDistrict();
		opm = new OldProvinceManager(ctx);
	}

	/**
	 * OldDistrictテーブル�?�らデータを�?��?��?��?{@link OldDistrict}
	 * 型�?��?列リスト�?�入れ�?��?�。
	 * 
	 * @throws SQLException
	 */
	private void loadListOldDistrict() throws SQLException {
		helper.open();

		String[] columns = { OldDistrict.COLUMN0, OldDistrict.COLUMN1,
				OldDistrict.COLUMN2, OldDistrict.COLUMN3, OldDistrict.COLUMN4,
				OldDistrict.COLUMN5, OldDistrict.COLUMN6, OldDistrict.COLUMN7,
				OldDistrict.COLUMN8, OldDistrict.COLUMN9, OldDistrict.COLUMN10,
				OldDistrict.COLUMN11, OldDistrict.COLUMN12,
				OldDistrict.COLUMN13, OldDistrict.COLUMN14 };
		Cursor c = helper.getData(OldDistrict.TABLE_NAME, columns, null, null,
				null); // con tro chi den dong' output cua
		// ham
		// query()
		c.moveToFirst(); // chuyen den dong dau tien
		// get cac dong ra va them vao arraylist

		while (c.isAfterLast() == false) {
			OldDistrict objOldDistrict = new OldDistrict(c.getString(0),
					c.getString(1), c.getString(2), c.getString(3),
					c.getInt(4), c.getString(5), c.getString(6),
					c.getString(7), c.getString(8), c.getString(9),
					c.getString(10), c.getString(11), c.getString(12),
					c.getString(13), c.getString(14));
			listOldDistrict.add(objOldDistrict);
			TagType wtf = new TagType(c.getPosition(), c.getString(14));
			listTag.add(wtf);
			c.moveToNext();
		}
		helper.close();
	}

	/**
	 * タッグ�?�探�?��?��?�。
	 * 
	 * @param tag
	 *            　ストリング型�?�入力
	 * @return　{@link OldDistrict}型�?��?列リスト型�?�変数
	 */
	@SuppressLint("DefaultLocale")
	public ArrayList<OldDistrict> searchByTag(String tag) {
		ArrayList<OldDistrict> result = new ArrayList<OldDistrict>();
		for (TagType t : listTag) {
			if (t.getTag().toLowerCase().contains(tag.toLowerCase())) {
				result.add(listOldDistrict.get(t.getPosition()));
			}
		}
		return result;
	}

	/**
	 * ゲッター
	 * 
	 * @return
	 */
	public ArrayList<OldDistrict> getListOldDistrict() {

		return listOldDistrict;
	}

	/**
	 * 「OldProvinceID]�?�探�?��?��?�。
	 * 
	 * @return　「OldProvince�?型�?��?列
	 */
	public ArrayList<OldDistrict> searchByOldProvince(String oldProvinceID) {
		ArrayList<OldDistrict> result = new ArrayList<OldDistrict>();
		for (OldDistrict od : listOldDistrict) {
			if (od.getOldProvince().equalsIgnoreCase(oldProvinceID)) {
				result.add(od);
			}
		}
		return result;
	}

	public OldProvince getFKOldProvince(String id) {
		return opm.searchByID(id);
	}

	/**
	 * 「id]�?�探�?��?��?�。
	 * 
	 * @return　１�?�「OldDistrict�?
	 */
	public OldDistrict searchByID(String id) {
		for (OldDistrict od : listOldDistrict) {
			if (od.getOldDistrictID().equalsIgnoreCase(id)) {
				return od;
			}
		}
		return null;
	}
}
